package com.ykdl.member.kid.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.ykdl.member.kid.models.CatalogArticleItem;
import com.ykdl.member.kid.models.CatalogBean;
import com.ykdl.member.kid.models.CatalogItemBean;
import com.ykdl.member.kid.models.CatalogTopicItem;

import net.wxxr.http.interfaces.IJsonParser;

public class HomeBean implements Serializable, IJsonParser{
	private static final long serialVersionUID = -254075575073094016L;
	
//	{
//		+doctor_talks: [ … ]
//		+group_catalogs: [ … ]
//		+top_ads: [ … ]
//		+topics: [ … ]
//		}
//	
	/** 资讯分类(AdItemBean)集合, 用户显示首页顶部图片轮播**/
	private List<AdItemBean>  top_ads = null;
	
	/** 医生说育儿 (包含文章和诊所精选)**/
	/**doctor_talks**/
	private List<ArticleBean>  articles = null;  // 文章
	private List<CatalogBean>  catalogs = null; // 诊所精选
	
	/**辣妈聊育儿 话题**/
	private List<TopicBean> topics = null;
	
	/** 热图榜 **/
	private List<CatalogBean> group_catalogs = null;

	public List<AdItemBean> getTop_ads() {
		if (top_ads == null){
			top_ads = new ArrayList<AdItemBean>();
		}
		
		return top_ads;
	}

	public void setTop_ads(List<AdItemBean> top_ads) {
		this.top_ads = top_ads;
	}

	public List<ArticleBean> getArticles() {
		if (articles == null){
			articles = new ArrayList<ArticleBean>();
		}
		return articles;
	}

	public void setArticles(List<ArticleBean> articles) {
		this.articles = articles;
	}

	public List<CatalogBean> getCatalogs() {
		if (catalogs == null){
			catalogs = new ArrayList<CatalogBean>();
		}
		return catalogs;
	}

	public void setCatalogs(List<CatalogBean> catalogs) {
		this.catalogs = catalogs;
	}

	public List<TopicBean> getTopics() {
		if (topics == null){
			topics = new ArrayList<TopicBean>();
		}
		return topics;
	}

	public void setTopics(List<TopicBean> topics) {
		this.topics = topics;
	}

	public List<CatalogBean> getGroup_catalogs() {
		if (group_catalogs == null){
			group_catalogs = new ArrayList<CatalogBean>();
		}
		return group_catalogs;
	}

	public void setGroup_catalogs(List<CatalogBean> group_catalogs) {
		this.group_catalogs = group_catalogs;
	}



	@Override
	public IJsonParser parse(String json) throws Exception {
		Gson   gson = null;
		if(null != json){
			final HomeBean  bean = new HomeBean();
			GsonBuilder b = new GsonBuilder();
			gson  = b.create();
			
			JSONObject  object = new JSONObject(json);
			//图片轮播
			JSONArray top_ads_json_array = object.getJSONArray("top_ads");
			//医生说育儿
			JSONArray doctor_talks_json_array = object.getJSONArray("doctor_talks");
			//辣妈聊育儿
			JSONArray group_catalogs_json_array = object.getJSONArray("group_catalogs");
			//热图榜
			JSONArray topics_json_array = object.getJSONArray("topics");
			
			//组建图片轮播集合
			if (top_ads_json_array != null && top_ads_json_array.length() > 0){
				for(int i = 0 ; i < top_ads_json_array.length() ; i++){
					String top_ad_json = top_ads_json_array.getString(i);
					AdItemBean aditem = gson.fromJson(top_ad_json, AdItemBean.class);
					bean.getTop_ads().add(aditem);
				}
			}
			
			
			//组建医生说育儿数据
			if(doctor_talks_json_array != null && doctor_talks_json_array.length() > 0){
				for(int i = 0 ; i < doctor_talks_json_array.length() ; i++){
					final String doctor_talk_item = doctor_talks_json_array.getString(i);
					final JSONObject  jo = new JSONObject(doctor_talk_item);
					if (jo.has("article_id") && !jo.isNull("article_id")){
						bean.getArticles().add(gson.fromJson(doctor_talk_item, ArticleBean.class));
					}else if(jo.has("catalog_id") && !jo.isNull("catalog_id")){
						final int catalog_type = jo.getInt("catalog_type");
						final String catalog_items = jo.getString("items");
						if(catalog_items == null || catalog_items.equals("[]")) continue;
						List<CatalogItemBean>  item = null;
						if(catalog_type == 1 || catalog_type == 3){ // 话题 或 问题
							item = gson.fromJson(catalog_items, new TypeToken<List<CatalogTopicItem>>(){}.getType());
							if(null == ((CatalogTopicItem)item.get(0)).getItem()) continue;
						}else if(jo.getInt("catalog_type") == 2){ //文章
							item = gson.fromJson(catalog_items, new TypeToken<List<CatalogArticleItem>>(){}.getType());
							if(null == ((CatalogArticleItem)item.get(0)).getItem()) continue;
						}
						final CatalogBean  catalog = gson.fromJson(doctor_talk_item, CatalogBean.class);
						if(item != null){
							catalog.setCatalogItems(item);
						}
						bean.getCatalogs().add(catalog);
					}
				}
			}

			//辣妈聊育儿
			if(group_catalogs_json_array !=null && group_catalogs_json_array.length() > 0){
				for(int i = 0 ; i < group_catalogs_json_array.length() ; i++){
					final String group_catalogs_item = group_catalogs_json_array.getString(i);
					if(group_catalogs_item == null || group_catalogs_item.equals("{}")) continue;
					final JSONObject  jo = new JSONObject(group_catalogs_item);
					if(jo.has("catalog_id") && !jo.isNull("catalog_id")){
						final int catalog_type = jo.getInt("catalog_type");
						final String catalog_items = jo.getString("items");
						if(catalog_items == null || catalog_items.equals("[]")) continue;
						List<CatalogItemBean>  item = null;
						if(catalog_type == 1 || catalog_type == 3){ // 话题 或 问题
							item = gson.fromJson(catalog_items, new TypeToken<List<CatalogTopicItem>>(){}.getType());
							if(null == ((CatalogTopicItem)item.get(0)).getItem()) continue;
						}else if(jo.getInt("catalog_type") == 2){ //文章
							item = gson.fromJson(catalog_items, new TypeToken<List<CatalogArticleItem>>(){}.getType());
							if(null == ((CatalogArticleItem)item.get(0)).getItem()) continue;
						}
						final CatalogBean  catalog = gson.fromJson(group_catalogs_item, CatalogBean.class);
						if(item != null){
							catalog.setCatalogItems(item);
						}
						bean.getCatalogs().add(catalog);
					}
				}
				
			}
			
			//组建热图
			if(topics_json_array != null && topics_json_array.length() > 0){
				for(int i = 0 ; i < topics_json_array.length() ; i++){
					bean.getTopics().add(gson.fromJson(topics_json_array.getString(i), TopicBean.class));
				}
			}

			
			return bean;
			
		}
		return null;
		
		
	}


}
